package cn.tmpan.service.impl;

import cn.tmpan.common.ServerResponse;
import cn.tmpan.dao.MemberMapper;
import cn.tmpan.dao.MsgRecordMapper;
import cn.tmpan.entity.Member;
import cn.tmpan.entity.MsgRecord;
import cn.tmpan.service.IUserService;
import cn.tmpan.util.MD5Util;
import cn.tmpan.util.RadomStrUtil;
import cn.tmpan.util.Sha1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by erhuo on 2018/3/10.
 */
@Service("iUserService")
public class UserserviceImpl implements IUserService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MsgRecordMapper msgRecordMapper;

    /**
     * 获取用户信息
     * @param uid 用户ID
     * @return   用户实体
     */
    @Override
    public ServerResponse<Member> getUserInfo(Integer uid) {
        Member member=memberMapper.selectByPrimaryKey(uid);
        if(member==null){
            return ServerResponse.createBySuccessMessage("用户不存在!");
        }
        return ServerResponse.createBySuccessMsgAndData("查询成功！",member);
    }

    public ServerResponse<Integer> getUserInfos() {
        int count=memberMapper.getUserCount();
        return ServerResponse.createBySuccessMsgAndData("查询成功！",count);
    }
    public ServerResponse login(Member member){
        Member retmember=null;

        if (member.getPassword()==null||member.getUsername()==null){
            return ServerResponse.createByErrorMessage("提交数据有误");
        }

        if(member.getUsername()!=null) {
            retmember = memberMapper.findUserByUserName(member.getUsername());
            if (retmember==null){
                return ServerResponse.createByErrorMessage("用户名不存在");
            }
        }

        String password = member.getPassword();
        password = Sha1.encode(MD5Util.MD5EncodeUtf8(password));
        System.out.println(MD5Util.MD5EncodeUtf8(password));
        System.out.println(password);
        System.out.println(retmember.getPassword());
        System.out.println(password.equals(retmember.getPassword()));
        System.out.println(password==retmember.getPassword());
        if (!password.equals(retmember.getPassword())){
            return  ServerResponse.createByErrorMessage("账号密码不正确");
        }
        //创建token
        String token = RadomStrUtil.getRandomStr(50);
        memberMapper.updateSidByUserName(retmember.getUsername(),token);

        retmember.setPassword("");
        retmember.setSid(token);
        return  ServerResponse.createBySuccessData(retmember);
    }

    @Override
    public ServerResponse<Member> reg(Member member) {
        Member retmember=null;

        if (member.getPassword()==null||member.getUsername()==null||member.getNickname()==null){
            return ServerResponse.createByErrorMessage("提交数据有误 请完善信息");
        }
        retmember = memberMapper.findUserByUserName(member.getUsername());
        if(retmember!=null){
            return  ServerResponse.createByErrorMessage("该用户名已存在");
        }

        member.setPassword(Sha1.encode(MD5Util.MD5EncodeUtf8(member.getPassword())));
        member.setPhone(null);
        memberMapper.insert(member);
        return  ServerResponse.createBySuccess();
    }

    /**
     * 绑定用户手机
     * @param phone 手机
     * @param code  验证码
     * @param uid   用户id
     * @return
     */
    @Override
    public ServerResponse bindPhone(String phone, String code, Integer uid) {
        MsgRecord msg = new MsgRecord();
        MsgRecord retmsg = new MsgRecord();
        msg.setSendTarget(phone);
        msg.setUid(uid);
        msg.setCode(code);
        retmsg = msgRecordMapper.selectRecentCodeByCodeTarget(msg);
        if(retmsg!=null){
            msgRecordMapper.deleteByPk(retmsg.getId());
            memberMapper.updatePhoneByPk(phone,uid);
            return  ServerResponse.createBySuccessMessage("保存成功");
        }else{
            return  ServerResponse.createByErrorMessage("验证码错误");
        }
    }

//    @Override
//    public Member getUserAuto(HttpServletRequest request){
//        Member ret = null;
//        String sid = request.getParameter("sid");
//        System.out.println(sid);
//        if (sid!=null) {
//            ret = memberMapper.selectUserBySid(sid);
//        }
//        return ret;
//    }
}
